//
//  CompanyJobtagViewController.m
//  Btpin
//
//  Created by Vic on 2019/4/2.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "CompanyJobtagViewController.h"
#import "LYSpecialLabelCell.h"
#import "UICollectionViewLeftAlignedLayout.h"
#import "LYSpecialLabelCell.h"
#import "PublicCategoryElementModel.h"

@interface CompanyJobtagViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateLeftAlignedLayout>
@property (nonatomic, strong) UICollectionView *collectview;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation CompanyJobtagViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"公司福利";
    
    [self loadData];
    [self initUI];
}
- (void)loadData {
    self.dataArray = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < [DataToolsControl sharedManager].allCategoryInfoModel.jobtagArray.count; i++) {
        PublicCategoryElementModel *model = [[DataToolsControl sharedManager].allCategoryInfoModel.jobtagArray[i] copy];
        model.isSelect = NO;
        if (self.tagArr && self.tagArr.count) {
            for (TagModel *mmodel in self.tagArr) {
                if ([mmodel.tag_id integerValue] == [model.id integerValue]) {
                    model.isSelect = YES;
                }
            }
        }
        model.labelid = i;
        [self.dataArray addObject:model];
    }
}
- (void)initUI {
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH-50*Scale, HeightOfStatusBar+3*Scale, 50*Scale, 36) imageName:nil target:self action:@selector(okBtnTap) title:@"确定" titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:okBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UILabel *topLbl = [UIToolsControl initLabelWithFrame:CGRectMake(15*Scale, 10*Scale, WIDTH-60*Scale, 40*Scale) text:@"最多可选5项" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_14];
    [self.view addSubview:topLbl];
    
    UICollectionViewLeftAlignedLayout *leftAlignedLayout = [[UICollectionViewLeftAlignedLayout alloc] init];
    leftAlignedLayout.minimumLineSpacing = 10*Scale;//最小行间距
    leftAlignedLayout.minimumInteritemSpacing = 15*Scale; //最小列间距
    _collectview = [[UICollectionView alloc] initWithFrame:CGRectMake(15*Scale, 50*Scale, WIDTH-30*Scale, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height) collectionViewLayout:leftAlignedLayout];
    _collectview.backgroundColor = COLOR_OF_WHITE;
    _collectview.showsHorizontalScrollIndicator = NO;
    _collectview.showsVerticalScrollIndicator = NO;
    _collectview.delegate = self;
    _collectview.dataSource = self;
    [_collectview registerClass:[LYSpecialLabelCell class] forCellWithReuseIdentifier:isCellID];
    [self.view addSubview:_collectview];
}
- (void)okBtnTap {
    BOOL hasSelect = NO;
    NSMutableArray *tags = [NSMutableArray arrayWithCapacity:0];
    NSMutableArray *modelTags = [NSMutableArray arrayWithCapacity:0];
    NSString *tag_cn = @"";
    for (PublicCategoryElementModel *model in self.dataArray) {
        if (model.isSelect) {
            hasSelect = YES;
            [tags addObject:model.id];
            tag_cn = [NSString stringWithFormat:@"%@ %@", model.name, tag_cn];
            TagModel *mmmodel = [[TagModel alloc] init];
            mmmodel.tag_id = model.id;
            mmmodel.tag_cn = model.name;
            [modelTags addObject:mmmodel];
        }
    }
    if (!hasSelect) {
        SVPHUDError(TagsAlertText);
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(companyJobtagViewControllerBackInfo:infoArray:modelArray:)]) {
        [self.delegate companyJobtagViewControllerBackInfo:tag_cn infoArray:tags modelArray:modelTags];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LYSpecialLabelCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:isCellID forIndexPath:indexPath];
    PublicCategoryElementModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    NSInteger count = 0;
    for (PublicCategoryElementModel *model in self.dataArray) {
        if(model.isSelect){
            count ++;
        }
    }
    
    PublicCategoryElementModel * model = self.dataArray[indexPath.row];
    if (count < 5) {
        model.isSelect = !model.isSelect;
        [collectionView reloadData];
    } else if (count == 5 && model.isSelect == YES) {
        model.isSelect = !model.isSelect;
        [collectionView reloadData];
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    PublicCategoryElementModel *model = self.dataArray[indexPath.row];
    return CGSizeMake(model.itemWidth, 30*Scale);
}

@end
